home *** CD-ROM | disk | FTP | other *** search
/ TeX 1995 July / TeX CD-ROM July 1995 (Disc 1)(Walnut Creek)(1995).ISO / graphics / gnuplot / scanner.c < prev    next >
C/C++ Source or Header  |  1993-09-15  |  11KB  |  396 lines

  1. #ifndef lint
  2. static char *RCSid = "$Id: scanner.c%v 3.50 1993/07/09 05:35:24 woo Exp $";
  3. #endif
  4.  
  5.  
  6. /* GNUPLOT - scanner.c */
  7. /*
  8.  * Copyright (C) 1986 - 1993   Thomas Williams, Colin Kelley
  9.  *
  10.  * Permission to use, copy, and distribute this software and its
  11.  * documentation for any purpose with or without fee is hereby granted, 
  12.  * provided that the above copyright notice appear in all copies and 
  13.  * that both that copyright notice and this permission notice appear 
  14.  * in supporting documentation.
  15.  *
  16.  * Permission to modify the software is granted, but not the right to
  17.  * distribute the modified code.  Modifications are to be distributed 
  18.  * as patches to released version.
  19.  *  
  20.  * This software is provided "as is" without express or implied warranty.
  21.  * 
  22.  *
  23.  * AUTHORS
  24.  * 
  25.  *   Original Software:
  26.  *     Thomas Williams,  Colin Kelley.
  27.  * 
  28.  *   Gnuplot 2.0 additions:
  29.  *       Russell Lang, Dave Kotz, John Campbell.
  30.  *
  31.  *   Gnuplot 3.0 additions:
  32.  *       Gershon Elber and many others.
  33.  * 
  34.  * There is a mailing list for gnuplot users. Note, however, that the
  35.  * newsgroup 
  36.  *    comp.graphics.gnuplot 
  37.  * is identical to the mailing list (they
  38.  * both carry the same set of messages). We prefer that you read the
  39.  * messages through that newsgroup, to subscribing to the mailing list.
  40.  * (If you can read that newsgroup, and are already on the mailing list,
  41.  * please send a message info-gnuplot-request@dartmouth.edu, asking to be
  42.  * removed from the mailing list.)
  43.  *
  44.  * The address for mailing to list members is
  45.  *       info-gnuplot@dartmouth.edu
  46.  * and for mailing administrative requests is 
  47.  *       info-gnuplot-request@dartmouth.edu
  48.  * The mailing list for bug reports is 
  49.  *       bug-gnuplot@dartmouth.edu
  50.  * The list of those interested in beta-test versions is
  51.  *       info-gnuplot-beta@dartmouth.edu
  52.  */
  53.  
  54. #include <stdio.h>
  55. #include <ctype.h>
  56. #include "plot.h"
  57.  
  58. #ifdef AMIGA_AC_5
  59. #define O_RDONLY    0
  60. int open(const char * _name, int _mode, ...);
  61. int close(int);
  62. #endif
  63.  
  64. #ifdef vms
  65.  
  66. #include stdio
  67. #include descrip
  68. #include errno
  69.  
  70. #define MAILBOX "PLOT$MAILBOX"
  71. #define pclose(f) fclose(f)
  72.  
  73. #endif /* vms */
  74.  
  75.  
  76. #define isident(c) (isalnum(c) || (c) == '_')
  77.  
  78. #ifndef STDOUT
  79. #define STDOUT 1
  80. #endif
  81.  
  82. #define LBRACE '{'
  83. #define RBRACE '}'
  84.  
  85. #define APPEND_TOKEN {token[t_num].length++; current++;}
  86.  
  87. #define SCAN_IDENTIFIER while (isident(expression[current + 1]))\
  88.                 APPEND_TOKEN
  89.  
  90. extern struct lexical_unit token[MAX_TOKENS];
  91.  
  92. static int t_num;    /* number of token I'm working on */
  93.  
  94. #ifndef AMIGA_SC_6_1
  95. char *strcat(), *strcpy(), *strncpy();
  96. #endif /* !AMIGA_SC_6_1 */
  97.  
  98. /*
  99.  * scanner() breaks expression[] into lexical units, storing them in token[].
  100.  *   The total number of tokens found is returned as the function value.
  101.  *   Scanning will stop when '\0' is found in expression[], or when token[]
  102.  *     is full.
  103.  *
  104.  *     Scanning is performed by following rules:
  105.  *
  106.  *    Current char    token should contain
  107.  *     -------------    -----------------------
  108.  *    1.  alpha,_    all following alpha-numerics
  109.  *    2.  digit    0 or more following digits, 0 or 1 decimal point,
  110.  *                0 or more digits, 0 or 1 'e' or 'E',
  111.  *                0 or more digits.
  112.  *    3.  ^,+,-,/    only current char
  113.  *        %,~,(,)
  114.  *        [,],;,:,
  115.  *        ?,comma
  116.  *    4.  &,|,=,*    current char; also next if next is same
  117.  *    5.  !,<,>    current char; also next if next is =
  118.  *    6.  ", '    all chars up until matching quote
  119.  *    7.  #        this token cuts off scanning of the line (DFK).
  120.  *
  121.  *            white space between tokens is ignored
  122.  */
  123. scanner(expression)
  124. char expression[];
  125. {
  126. register int current;    /* index of current char in expression[] */
  127. register int quote;
  128. char brace;
  129.  
  130.     for (current = t_num = 0;
  131.         t_num < MAX_TOKENS && expression[current] != '\0';
  132.         current++) {
  133. again:
  134.         if (isspace(expression[current]))
  135.             continue;                        /* skip the whitespace */
  136.         token[t_num].start_index = current;
  137.         token[t_num].length = 1;
  138.         token[t_num].is_token = TRUE;    /* to start with...*/
  139.  
  140.         if (expression[current] == '`') {
  141.             substitute(&expression[current],MAX_LINE_LEN - current);
  142.             goto again;
  143.         }
  144.         /* allow _ to be the first character of an identifier */
  145.         if (isalpha(expression[current]) || expression[current] == '_') {
  146.             SCAN_IDENTIFIER;
  147.         } else if (isdigit(expression[current]) || expression[current] == '.'){
  148.             token[t_num].is_token = FALSE;
  149.             token[t_num].length = get_num(&expression[current]);
  150.             current += (token[t_num].length - 1);
  151.         } else if (expression[current] == LBRACE) {
  152.             token[t_num].is_token = FALSE;
  153.             token[t_num].l_val.type = CMPLX;
  154. #ifdef __PUREC__
  155.             { char    l[80];
  156.             if ((sscanf(&expression[++current],"%lf,%lf%[ }]s",
  157.                 &token[t_num].l_val.v.cmplx_val.real,
  158.                 &token[t_num].l_val.v.cmplx_val.imag,
  159.                 &l)    != 3) || (!strchr(l, RBRACE))  )
  160.                     int_error("invalid complex constant",t_num);
  161.             }
  162. #else
  163.             if ((sscanf(&expression[++current],"%lf , %lf %c",
  164.                 &token[t_num].l_val.v.cmplx_val.real,
  165.                 &token[t_num].l_val.v.cmplx_val.imag,
  166.                 &brace) != 3) || (brace != RBRACE))
  167.                     int_error("invalid complex constant",t_num);
  168. #endif
  169.             token[t_num].length += 2;
  170.             while (expression[++current] != RBRACE) {
  171.                 token[t_num].length++;
  172.                 if (expression[current] == '\0')            /* { for vi % */
  173.                     int_error("no matching '}'", t_num);
  174.             }
  175.         } else if (expression[current] == '\'' || expression[current] == '\"'){
  176.             token[t_num].length++;
  177.             quote = expression[current];
  178.             while (expression[++current] != quote) {
  179.                 if (!expression[current]) {
  180.                     expression[current] = quote;
  181.                     expression[current+1] = '\0';
  182.                     break;
  183.                 } else
  184.                     token[t_num].length++;
  185.             }
  186.         } else switch (expression[current]) {
  187.              case '#':        /* DFK: add comments to gnuplot */
  188.                   goto endline; /* ignore the rest of the line */
  189.             case '^':
  190.             case '+':
  191.             case '-':
  192.             case '/':
  193.             case '%':
  194.             case '~':
  195.             case '(':
  196.             case ')':
  197.             case '[':
  198.             case ']':
  199.             case ';':
  200.             case ':':
  201.             case '?':
  202.             case ',':
  203.                 break;
  204.             case '&':
  205.             case '|':
  206.             case '=':
  207.             case '*':
  208.                 if (expression[current] == expression[current + 1])
  209.                     APPEND_TOKEN;
  210.                 break;
  211.             case '!':
  212.             case '<':
  213.             case '>':
  214.                 if (expression[current + 1] == '=')
  215.                     APPEND_TOKEN;
  216.                 break;
  217.             default:
  218.                 int_error("invalid character",t_num);
  219.             }
  220.         ++t_num;    /* next token if not white space */
  221.     }
  222.  
  223. endline:                    /* comments jump here to ignore line */
  224.  
  225. /* Now kludge an extra token which points to '\0' at end of expression[].
  226.    This is useful so printerror() looks nice even if we've fallen off the
  227.    line. */
  228.  
  229.         token[t_num].start_index = current;
  230.         token[t_num].length = 0;
  231.     return(t_num);
  232. }
  233.  
  234.  
  235. get_num(str)
  236. char str[];
  237. {
  238. double atof();
  239. register int count = 0;
  240. long atol();
  241. register long lval;
  242.  
  243.     token[t_num].is_token = FALSE;
  244.     token[t_num].l_val.type = INTGR;        /* assume unless . or E found */
  245.     while (isdigit(str[count]))
  246.         count++;
  247.     if (str[count] == '.') {
  248.         token[t_num].l_val.type = CMPLX;
  249.         while (isdigit(str[++count]))    /* swallow up digits until non-digit */
  250.             ;
  251.         /* now str[count] is other than a digit */
  252.     }
  253.     if (str[count] == 'e' || str[count] == 'E') {
  254.         token[t_num].l_val.type = CMPLX;
  255. /* modified if statement to allow + sign in exponent
  256.    rjl 26 July 1988 */
  257.         count++;
  258.         if (str[count] == '-' || str[count] == '+')
  259.             count++;
  260.         if (!isdigit(str[count])) {
  261.             token[t_num].start_index += count;
  262.             int_error("expecting exponent",t_num);
  263.         }
  264.         while (isdigit(str[++count]))
  265.             ;
  266.     }
  267.     if (token[t_num].l_val.type == INTGR) {
  268.          lval = atol(str);
  269.         if ((token[t_num].l_val.v.int_val = lval) != lval)
  270.             int_error("integer overflow; change to floating point",t_num);
  271.     } else {
  272.         token[t_num].l_val.v.cmplx_val.imag = 0.0;
  273.         token[t_num].l_val.v.cmplx_val.real = atof(str);
  274.     }
  275.     return(count);
  276. }
  277.  
  278. #if defined(unix) || defined(vms) || defined(PIPES) || (defined(ATARI) && defined(__PUREC__))
  279.  
  280. substitute(str,max)            /* substitute output from ` ` */
  281. char *str;
  282. int max;
  283. {
  284. register char *last;
  285. register int i,c;
  286. register FILE *f;
  287. #ifdef AMIGA_AC_5
  288. int fd;
  289. #else
  290. #if defined(ATARI) && defined(__PUREC__)
  291. char    *atari_tmpfile;
  292. char    *atari_pgm[MAX_LINE_LEN+100];
  293. #else
  294. FILE *popen();
  295. #endif /* ATARI && PUREC */
  296. #endif
  297. static char pgm[MAX_LINE_LEN+1],output[MAX_LINE_LEN+1];
  298.  
  299. #ifdef vms
  300. int chan;
  301. static $DESCRIPTOR(pgmdsc,pgm);
  302. static $DESCRIPTOR(lognamedsc,MAILBOX);
  303. #endif /* vms */
  304.  
  305.     i = 0;
  306.     last = str;
  307.     while (*(++last) != '`') {
  308.         if (*last == '\0')
  309.             int_error("unmatched `",t_num);
  310.         pgm[i++] = *last;
  311.     }
  312.     pgm[i] = '\0';        /* end with null */
  313.     max -= strlen(last);    /* max is now the max length of output sub. */
  314.   
  315. #ifdef vms
  316.       pgmdsc.dsc$w_length = i;
  317.        if (!((vaxc$errno = sys$crembx(0,&chan,0,0,0,0,&lognamedsc)) & 1))
  318.            os_error("sys$crembx failed",NO_CARET);
  319.    
  320.        if (!((vaxc$errno = lib$spawn(&pgmdsc,0,&lognamedsc,&1)) & 1))
  321.            os_error("lib$spawn failed",NO_CARET);
  322.    
  323.        if ((f = fopen(MAILBOX,"r")) == NULL)
  324.            os_error("mailbox open failed",NO_CARET);
  325. #else /* vms */
  326. #if defined(ATARI) && defined(__PUREC__)
  327.         if (system(NULL) == 0)
  328.             os_error("no command shell");
  329.         if ((strlen(atari_tmpfile) + strlen(pgm) + 5) > MAX_LINE_LEN+100)
  330.             os_error("sorry, command to long");
  331.         atari_tmpfile = tmpnam(NULL);
  332.         strcpy(atari_pgm, pgm);
  333.         strcat(atari_pgm, " >> ");
  334.         strcat(atari_pgm, atari_tmpfile);
  335.         system(atari_pgm);
  336.         if ((f = fopen(atari_tmpfile, "r")) == NULL)
  337. #else
  338. #ifdef AMIGA_AC_5
  339.       if ((fd = open(pgm,"O_RDONLY")) == -1)
  340. #else
  341.       if ((f = popen(pgm,"r")) == NULL)
  342. #endif
  343. #endif    /* ATARI && PUREC */
  344.           os_error("popen failed",NO_CARET);
  345. #endif /* vms */
  346.  
  347.     i = 0;
  348.     while ((c = getc(f)) != EOF) {
  349.         output[i++] = ((c == '\n') ? ' ' : c);    /* newlines become blanks*/
  350.         if (i == max) {
  351. #ifdef AMIGA_AC_5
  352.             (void) close(fd);
  353. #else
  354. #if defined(ATARI) && defined(__PUREC__)
  355.             (void) fclose(f);
  356.             (void) unlink(atari_tmpfile);
  357. #else
  358.             (void) pclose(f);
  359. #endif /* ATARI && PUREC */
  360. #endif
  361.             int_error("substitution overflow", t_num);
  362.         }
  363.     }
  364. #ifdef AMIGA_AC_5
  365.     (void) close(fd);
  366. #else
  367. #if defined(ATARI) && defined(__PUREC__)
  368.     (void) fclose(f);
  369.     (void) unlink(atari_tmpfile);
  370. #else
  371.     (void) pclose(f);
  372. #endif /* ATARI && PUREC */
  373. #endif
  374.  
  375.     if (i + strlen(last) > max)
  376.         int_error("substitution overflowed rest of line", t_num);
  377.     (void) strncpy(output+i,last+1,MAX_LINE_LEN-i);
  378.                                     /* tack on rest of line to output */
  379.     (void) strcpy(str,output);                /* now replace ` ` with output */
  380.     screen_ok = FALSE;
  381. }
  382.  
  383. #else /* unix || vms || PIPES || ATARI && PUREC */
  384.  
  385. #ifdef __ZTC__
  386. substitute(char *str,int max)
  387. #else
  388. substitute()
  389. #endif
  390. {
  391.     char line[100];
  392.  
  393.     int_error( strcat(strcpy(line,"substitution not supported by "),OS),t_num);
  394. }
  395. #endif /* unix || vms || PIPES || ATARI && PUREC */
  396.